package com.ihr360.payroll.model.dto.salary.report;

import com.ihr360.commons.model.entity.TimeWithCompanyId;
import com.ihr360.payroll.model.base.salaryreport.IPayPeriod;
import com.ihr360.payroll.model.dto.salary.task.period.PayPeriodTerm;
import com.ihr360.payroll.type.PeriodState;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Eric on 2018/4/11/011.
 */
@Entity
@Table(
        indexes = {@Index(name = "company_index", columnList = "companyId")},
        uniqueConstraints = @UniqueConstraint(columnNames = {"companyId", "year", "month", "salaryPlan_id"}))
public class HistoryPayPeriod extends TimeWithCompanyId implements IPayPeriod {
    //与活跃薪酬账期id保持一致
    @Id
    private Long id;

    /**
     * 薪资方案(快照记录)
     */
    @ManyToOne
    private HistorySalaryPlan salaryPlan;
    /**
     * 薪资周期
     */
    @Column(nullable = false, columnDefinition = "DATE")
    private Date startOn;

    @Column(nullable = false, columnDefinition = "DATE")
    private Date endOn;


    private int year;

    private int month;


    /**
     * 考勤周期
     */
    @Column(nullable = false, columnDefinition = "DATE")
    private Date attendanceStartOn;

    @Column(nullable = false, columnDefinition = "DATE")
    private Date attendanceEndOn;

    @OneToMany(mappedBy = "historyPayPeriod", fetch = FetchType.EAGER,
            cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
    @OrderBy("startOn")
    private List<PayPeriodTerm> terms = new ArrayList<>();

    @Enumerated(EnumType.STRING)
    @Column(nullable = false, length = 40)
    private PeriodState state = PeriodState.CLOSED;

    /**
     * 关联审批记录id
     * */
    @Column(length = 40)
    private String applicationId;

    public String getApplicationId() {
        return applicationId;
    }

    public void setApplicationId(String applicationId) {
        this.applicationId = applicationId;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    public int getYear() {
        if (year == 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startOn);
            year = calendar.get(Calendar.YEAR);
        }
        return year;
    }

    public int getMonth() {
        if (month == 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startOn);
            month = calendar.get(Calendar.MONTH) + 1;
        }
        return month;
    }

    public void setYear(int year) {
        if (year == 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startOn);
            year = calendar.get(Calendar.YEAR);
        }
        this.year = year;
    }

    public void setMonth(int month) {
        if (month == 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startOn);
            month = calendar.get(Calendar.MONTH) + 1;
        }
        this.month = month;
    }

    public HistorySalaryPlan getSalaryPlan() {
        return salaryPlan;
    }

    public void setSalaryPlan(HistorySalaryPlan salaryPlan) {
        this.salaryPlan = salaryPlan;
    }

    public Date getStartOn() {
        return startOn;
    }

    public void setStartOn(Date startOn) {
        this.startOn = startOn;
    }

    public Date getEndOn() {
        return endOn;
    }

    public void setEndOn(Date endOn) {
        this.endOn = endOn;
    }

    public Date getAttendanceStartOn() {
        return attendanceStartOn;
    }

    public void setAttendanceStartOn(Date attendanceStartOn) {
        this.attendanceStartOn = attendanceStartOn;
    }

    public Date getAttendanceEndOn() {
        return attendanceEndOn;
    }

    public void setAttendanceEndOn(Date attendanceEndOn) {
        this.attendanceEndOn = attendanceEndOn;
    }

    public List<PayPeriodTerm> getTerms() {
        return terms;
    }

    public void setTerms(List<PayPeriodTerm> terms) {
        this.terms = terms;
    }

    public PeriodState getState() {
        return state;
    }

    public void setState(PeriodState state) {
        this.state = state;
    }
}
